<template>
  <div>
    <el-dialog
  :title="obj.vAdd?'管理员编辑':'管理员添加'"
  :visible.sync="obj.isTrue"
  width="50%"
  @close="cancel()"
 >
  
<el-form ref="form" :model="form" label-width="80px">
 <el-form-item label="所属角色">
    <el-select v-model="form.roleid" placeholder="请选择所属角色">
      <el-option  v-for="(item,index) in rolelist" :key="index" :value="item.id" :label="item.rolename"></el-option>
     
    </el-select>
  </el-form-item>
 <el-form-item label="用户名">
    <el-input v-model="form.username"></el-input>
  </el-form-item>
   <el-form-item label="密码">
    <el-input v-model="form.password"></el-input>
  </el-form-item>
   <el-form-item label="状态">
    <el-switch v-model="form.status" :active-value="1" :inactive-value=2></el-switch>{{form.status}}
  </el-form-item>
</el-form>

  <span slot="footer" class="dialog-footer">
    <el-button @click="cancel()">取 消</el-button>
     <el-button v-if="obj.vAdd" @click="update()"  type="primary">编辑</el-button>
    <el-button v-else type="primary" @click="add()">添加</el-button>
   
  </span>
</el-dialog>

  </div>
</template>

<script>
import {mapActions,mapGetters} from "vuex"
import {useradd,userinfo,useredit} from "../../../request/api.js"
import { successAlert, warningAlert } from "../../../utils/alert.js";
export default {
props: ["obj"],
data() {
  return {
    form:{
      roleid:"",
      username:"",
      password:"",
status:1
    },
  };
},
  computed:{
  ...mapGetters({
    rolelist:"role/list"
  })
},
methods:{
   ...mapActions({
    cc:"role/asyncChangeList",
     manageUser: "user/asyncChangeList",
      asyncMangeTotal:"user/asyncChangeTotal"
  }),
  //清空并关闭
empty(){
  this.form={
    roleid:"",
      username:"",
      password:"",
status:1
  },
this.obj.isTrue=false
},
//取消
cancel(){
  this.empty()
},
//添加
add(){
  useradd(this.form).then(res=>{
    if(res.data.code==200){
      successAlert(res.data.msg)
      this.manageUser()
      this.asyncMangeTotal()
    }else{
       warningAlert(res.data.msg)
    }
  })
  this.empty()
},
//获取一条信息
getOne(uid){
userinfo({uid:uid}).then(res=>{
  this.form=res.data.list
  this.form.password=""
})
},
//确认编辑
update(){
  useredit(this.form).then(res=>{
    if(res.data.code==200){
      successAlert(res.data.msg)
      this.manageUser()
      this.empty()
    }else{
      warningAlert(res.data.msg)
    }
  })
  
  
}
},
mounted(){
//调用role角色列表
this.cc()
}
}

</script>

<style>

</style>